#!/user/bin/env python3
#! -*- coding: utf-8 -*-
__author__ ="jzp";
import re;
import os;
import pymysql;
from base.python_base_02 import *;
from util.pymysql_util import  *;
from datetime import datetime;
'''
python练习
'''

from base.python_base_02 import *;


# ==========模块调用============
def fun_10():
    #python_base_02.fun_01();
    print(User("jzp","123456","110.com").__dict__);


# =========异常处理=========
def fun_11():
    try:
        for i in list(range(1,10)):
            print(i);
        a = 1 / 1;
    except BaseException as be:
       print(be);
    finally:
       print("必须执行的代码");


# =========文件读写=========
def fun_12():
    f=open(file="H:/APEX/text.txt",mode="w",encoding="utf-8");
    f.writelines(["sdsefw","\n","ewfewegfewg","\n"]);
    f.close();

def fun_13():
    f=None;
    try:
        f=open(file="H:/APEX/text.txt",mode="r",encoding="utf-8");
        results = f.readlines();
        print(results);
    except BaseException as e:
        print(e);
    finally:
        if f:
            f.close();

def fun_14():
    with open(file="H:/APEX/text.txt",mode="r",encoding="utf-8") as f:
        print(f.readlines());

def fun_15():
    with open(file="H:/APEX/text.txt",mode="a",encoding="utf-8") as f:
        f.writelines(["追加第一行","\n","追加第二行","\n","追加第三行"]);


# ============ OS操作 ============
def fun_16():
    print(os.name);
    dest_folder = "H:/XboxGames/text.txt/sfef"
    if not os.path.exists(dest_folder):
        os.makedirs(dest_folder);
        os.rename("H:/XboxGames/text.txt","H:/XboxGames/txt.txt")


# ======== mysql操作 ========
def fun_17():
    # sql 语句
    insert_sql = insert_sql = 'insert into user (user_name, password, create_date) ' \
                'values("%s", "%s", "%s")' % ("jzp", "170714", datetime.now().strftime("%Y-%m-%d %H:%M:%S"));
    query_sql = "select * from user";

    connection, cursor = None, None;
    try:
        # 获取链接对象、游标对象
        connection, cursor = get_connection_cursor();
        # 执行新增操作
        num = execute_(insert_sql, cursor);
        print(num);
        # 提交事务
        commit_(connection);

        # 执行查询操作
        result = query_(query_sql, cursor);
        print(result);
    except BaseException as e:
        print(e);
        # 回滚事务
        rollback_(connection);
    finally:
        # 关闭连接和游标
        close_(connection, cursor);


# ======== json ========
def fun_18():
    # json 字符串转 dict
    j = '{"name":"hj", "age":19, "gender":"male"}';
    print(j);
    print(json.loads(j));
    # dict 转 json
    d = {"color":"red", "name":"table", "price":34.45, "count":33};
    print(d);
    print(json.dumps(d));
    # 对象转 json 字符串
    user = User(user_name="hahhh", password="111111", email="110.com");
    print(json.dumps(user.__dict__));
    # json 字符串转对象
    j = '{"user_name": "sss", "password": "2222", "email": "aa@163.com"}';
    user.__dict__ = json.loads(j);
    print(user.__dict__);

# ======== datetime ========
def fun_19():
    print(datetime.now());
    print(datetime.now().timestamp());
    print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"));
    print(datetime.strptime("2023-05-10 16:18:11", "%Y-%m-%d %H:%M:%S"));

# ======== hashlib ========
def fun_20(password):
    password = password + "abc";
    print(hashlib.md5(password.encode("utf-8")).hexdigest());

if __name__ == '__main__':
    fun_20();

